<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="isHide ? 1 : 6" :xs="24" class="relative">
        <div class="app-container-tree-border">
          <project-tree-select-inner class="cd-mb8" :projectId="projectId" @changeProject="changeProject" />
          <el-input class="cd-mb8" v-model="filterText" placeholder="请输入" clearable size="small"
            prefix-icon="el-icon-search" />
          <el-tree class="input-tree" :data="treeList" :props="defaultProps" ref="tree" node-key="id" highlight-current
            @node-click="handleNodeClick" :filter-node-method="filterNode" :default-expanded-keys="defaultExpandedKeys">
            <span slot-scope="{ node, data }" class="el-tree-custom-node">
              <span class="node-label" :class="{ 'red': data.type == 'zd' && data.hasGx == 0 }" :title="node.label">
                {{ node.label }}
              </span>
              <span class="can-remove" v-if="data.type == 'fbfx'">
                <el-button type="text" size="mini" v-hasPermi="['base:bsFbfx:edit']"
                  @click.native.stop="onUpdateFbfx(data)">
                  修改
                </el-button>
                <el-popconfirm title="您确定要删除该目录吗？" icon-color="red" class="m-r-xs" v-hasPermi="['base:bsFbfx:remove']"
                  @confirm="onDeleteFbfx(data)">
                  <el-button slot="reference" type="text" size="mini">
                    删除
                  </el-button>
                </el-popconfirm>
              </span>
            </span>
          </el-tree>
          <div class="tree-hide-button iconfont" :class="isHide ? 'icon-xiangyou' : 'icon-xiangzuo'"
            @click="() => { isHide = !isHide }">
          </div>
        </div>
      </el-col>
      <el-col :span="isHide ? 23 : 18" :xs="24">
        <div class="app-container-table-border">
          <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px" class="top-left">
            <el-form-item label="报验时间" prop="reportDate">
              <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                range-separator="～" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="审批状态" prop="approveStatus">
              <el-select v-model="queryParams.approveStatus" placeholder="请选择审批状态" @change="handleQuery">
                <el-option v-for="dict in dict.type.report_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form> -->
          <el-row class="mb8 top-right-btn small-line-height">
            <el-button v-if="treeInfo.id && (treeInfo.type == 'zd' || treeInfo.type == 'fbfx') && dataList.length == 0"
              type="success" plain size="mini" @click="onAddFbfx" v-hasPermi="['base:bsFbfx:add']">新增分部分项</el-button>
            <el-button v-if="treeInfo && treeInfo.id && treeInfo.type == 'fbfx' && treeInfo.children.length == 0"
              type="primary" plain size="mini" @click="onAddGx"
              v-hasPermi="['cm:bsProcessInspection:add']">新增工序</el-button>
          </el-row>
          <template v-if="treeInfo.id">
            <el-card class="card-border" shadow="always">
              <el-descriptions title="" :column="3" labelClassName="descriptions-label"
                contentClassName="descriptions-content">
                <el-descriptions-item label="站点名称">{{ topInfo.zdName }}</el-descriptions-item>
                <el-descriptions-item label="容量(KW)">{{ topInfo.capacity }}</el-descriptions-item>
                <el-descriptions-item label="地点">{{ topInfo.address }}</el-descriptions-item>
                <el-descriptions-item label="建设单位">{{ topInfo.jiansdwName }}</el-descriptions-item>
                <el-descriptions-item label="施工单位">{{ topInfo.sgdwName }}</el-descriptions-item>
                <el-descriptions-item label="监理单位">{{ topInfo.jldwName }}</el-descriptions-item>
              </el-descriptions>
            </el-card>
            <el-card class="card-border cd-mt8" shadow="always">
              <el-steps v-if="treeInfo.zdId && selectGxId" :active="selectGxActive" align-center>
                <el-step title="施工单位报审"></el-step>
                <el-step title="施工缺陷整改"></el-step>
                <el-step title="监理答复"></el-step>
              </el-steps>
              <el-divider class="divider" v-if="treeInfo.zdId && selectGxId"></el-divider>
              <el-row v-if="dataList.length > 0">
                <el-col :span="6">
                  <div class="gx-li" :class="{ 'on': selectGxId && selectGxId == item.id }"
                    v-for="(item, index) in dataList" :key="index" @click="selectGxIdChange(item.id)">
                    <span class="circle">{{ index + 1 }}</span>
                    <span class="name">{{ item.name }}</span>
                  </div>
                </el-col>
                <el-col :span="16">
                  <gxby-add-or-update v-if="treeInfo.zdId && selectGxId" :id="selectGxId"
                    :selectGxActive="selectGxActive" ref="gxbyAddOrUpdateRef" @callBack="getList" @onExamine="onExamine"
                    @onDel="onDelGx" />
                </el-col>
              </el-row>
              <el-empty v-else description="暂无数据" :image-size="200"></el-empty>
            </el-card>
          </template>
        </div>
      </el-col>
    </el-row>
    <fbfx-add-or-update ref="fbfxAddOrUpdateRef" @callBack="getFbfxTree" :zdId="treeInfo.zdId" />
    <gx-add ref="gxAddRef" @callBack="getList" :zdId="treeInfo.zdId" />
    <!-- <gxby-add-or-update ref="gxbyAddOrUpdateRef" @callBack="getList" /> -->
    <gxby-view-dialog ref="gxbyViewDialogRef" />
    <examine-and-approve ref="examineAndApproveRef" @callBack="handleQuery" />
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { fbfxTreelist, delBsFbfx } from "@/api/pl/bsFbfx";
import { listBsProcessInspection, delBsProcessInspection, checkProcessInspection, getBsProcessInspection, updateBsProcessInspection } from "@/api/pl/bsProcessInspection";
import { queryBdInfoByZdId } from "@/api/base/bsBd";
import decimal from "@/utils/big-decimal";

import FbfxAddOrUpdate from './fbfx-add-or-update.vue';
import GxAdd from './gx-add.vue';
import GxbyAddOrUpdate from './gxby-add-or-update.vue';
import GxbyViewDialog from './gxby-view-dialog.vue';
import ExamineAndApprove from './examine-and-approve.vue';
export default {
  name: 'ProcessInspection',
  dicts: ['report_status'],
  components: {
    FbfxAddOrUpdate,
    GxAdd,
    GxbyAddOrUpdate,
    GxbyViewDialog,
    ExamineAndApprove
  },
  data () {
    return {
      isHide: false,
      treeInfo: {},
      oldZdId: undefined,
      treeList: [],
      defaultExpandedKeys: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      filterText: undefined,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        ipaddr: undefined,
        userName: undefined,
        status: undefined,
      },
      form: {},
      dataList: [],
      topInfo: {},
      selectGxInfo: {},
      selectGxId: undefined,
      selectGxActive: undefined,
      projectId: undefined
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      // 系统顶部选中项目变化
      this.changeProject(newVal);
    },
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  created () {
    this.changeProject();
  },
  mounted () {
  },
  methods: {
    changeProject (projectId = this.sysProjectId) {
      this.projectId = projectId;
      this.projectChangeResetQuery();
      this.getFbfxTree();
    },
    getFbfxTree () {
      fbfxTreelist({ projectId: this.projectId, hasGx: 1 }).then(response => {
        this.treeList = response.data;
        if (this.treeInfo && this.treeInfo.id) {
          this.defaultExpandedKeys = [this.treeInfo.parentId, this.treeInfo.id];
        }
        this.treeInfo = {};
      });
    },
    filterNode (value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick (data) {
      this.selectGxActive = 0;
      this.selectGxId = undefined;
      if (data.type == 'zd' || data.type == 'fbfx') {
        this.treeInfo = data;
        this.queryParams.fbfxId = data.id;
        if (data.type == 'zd' || data.children.length > 0) {
          this.dataList = [];
        } else {
          this.handleQuery();
        }
        if (this.treeInfo.zdId != this.oldZdId) {
          this.getBdInfoByZdId();
        }
        this.oldZdId = data.zdId;
      } else {
        this.treeInfo = {};
      }
    },
    projectChangeResetQuery () {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        bdId: undefined,
        zdId: undefined,
        parentId: undefined,
      };
      this.total = 0;
      this.dataList = [];
    },
    // 根据站点获取标段详情
    getBdInfoByZdId () {
      queryBdInfoByZdId(this.treeInfo.zdId).then(res => {
        this.topInfo = res.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery (selectGxId) {
      this.selectGxId = selectGxId;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      listBsProcessInspection(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.rows;
        if (this.dataList.length > 0 && !this.selectGxId) {
          this.selectGxIdChange(this.dataList[0].id);
        } else {
          this.selectGxIdChange(this.selectGxId);
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 选中的工序改变
    selectGxIdChange (id) {
      this.selectGxId = id;
      if (!id) {
        return;
      }
      const info = this.dataList.find(item => item.id == this.selectGxId);
      this.selectGxInfo = info;
      let selectGxActive = 0;
      switch (this.selectGxInfo.approveStatus) {
        case '00':
          selectGxActive = 0;
          break;
        case '11':
          selectGxActive = 2;
          break;
        case '22':
          selectGxActive = 3;
          break;
        case '33':
          selectGxActive = 1;
          break;

      }
      this.selectGxActive = selectGxActive;
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
    },
    // 表单重置
    reset () {
      this.form = {
        projectId: undefined,
      };
      this.resetForm("form");
    },
    // 新增分部分项
    onAddFbfx () {
      const info = {
        parentId: this.treeInfo.id,
        id: undefined
      };
      this.$refs.fbfxAddOrUpdateRef.init(info);
    },
    // 修改分部分项
    onUpdateFbfx (row) {
      const { id } = row;
      const info = {
        parentId: undefined,
        id: id
      };
      this.$refs.fbfxAddOrUpdateRef.init(info);
    },
    // 删除分部分项
    onDeleteFbfx (row) {
      delBsFbfx(row.id).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getFbfxTree();
      });
    },
    // 新增工序
    onAddGx () {
      this.$refs.gxAddRef.init(this.treeInfo.id);
    },
    // 删除工序
    onDelGx (row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除该工序？').then(() => {
        this.loading = true;
        return delBsProcessInspection(ids);
      }).then(() => {
        this.loading = false;
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 资料维护
    dataMaintenance (row) {
      this.$refs.gxbyAddOrUpdateRef.init(row.id);
    },
    onView (row) {
      this.$refs.gxbyViewDialogRef.init(row.id);
    },
    // 是否可操作
    canOperation (row) {
      return row.approveStatus == '00' || row.approveStatus == '33';
    },
    /** 发起审批 */
    async onExamine (row) {
      try {
        await checkProcessInspection(row.id);
        const projectInfo = this.sysProjectList.find(item => item.projectId == this.projectId);
        const info = {
          projectId: this.projectId,
          projectName: projectInfo.projectName,
          zdId: row.zdId,
          fbfxId: row.fbfxId,
          id: row.id,
          processInspectionName: row.name,
        };
        this.$refs.examineAndApproveRef.init(info);
      } catch (error) {
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.card-border {
  position: relative;
  width: 100%;
}


.divider {
  margin: 10px 0;
}

::v-deep {
  .descriptions-label {
    width: 80px;
    display: inline-block;
    text-align: right !important;
  }

  .descriptions-content {
    // padding: 5px !important;

    .el-form-item {
      margin-bottom: 0px !important;
    }
  }
}

.gx-li {
  display: flex;
  align-items: center;
  cursor: pointer;

  &.on {
    .circle {
      background-color: #409EFF !important;
      border: 1px solid #409EFF !important;
      color: #ffffff !important;
    }

    .name {
      color: #333;
    }
  }

  .circle {
    margin: 6px 10px 6px 0;
    display: inline-block;
    width: 40px;
    min-width: 40px;
    height: 40px;
    min-height: 40px;
    display: flex;
    align-content: center;
    align-items: center;
    justify-content: center;
    background-color: #ffffff;
    border-radius: 50%;
    border: 2px solid #b3b3b3;
    color: #b3b3b3;
    font-size: 16px;
  }

  .name {
    color: #b3b3b3;
  }
}
</style>
